<?php
	include("../connect/connect.php");

	$tempY=date("Y");
	if($tempY<"2550"){
		$tempY=(int)$tempY+543;
	}
	//$datetime_now= $tempY.date("-m-d-H:i");
	$datetime_now= $tempY.date("md");
	// เพิ่มข้อมูลใน event
	$sql = "select concat('25',substr(event_id,1,6)) as evndte,substr(event_id,17,4) as evntme,substr(event_id,8,6) as evnplcidgen,substr(event_id,14,2) as evnvllnogen,event_des as evndes,point_x,point_y,pol_recid,pol_cause,code_acc,mil_cause,pol_typ,type_acc1,type_acc2,type_acc3,event_id from event_daily";
	$rs=mysql_query($sql);
	//echo "<br>".$sql;
	while($row = mysql_fetch_array($rs)){//วนลูปเพิ่มเหตุการณ์จาก event_daily ไปที่ event
		//get pk  --> evnid
		$sql = "select max(evnid)+1 as pk_next from event";
		//echo "<br>".$sql;
		$rs2=mysql_query($sql);

		$row2=mysql_fetch_array($rs2);
		$evnid=$row2["pk_next"];
		
		$evndte=$row["evndte"];
		$evntme=$row["evntme"];
		$evnplcidgen=$row["evnplcidgen"];
		$evnvllnogen=$row["evnvllnogen"];
		$evndes=$row["evndes"];
		$tarx=$row["point_x"];
		$tary=$row["point_y"];
		//insert event
		$sql="insert into event(evnid,evndte,evntme,evnplcidgen,evnvllnogen,evndes,tarx,tary,savofc,savdte,updofc,upddte) values('$evnid','$evndte','$evntme','$evnplcidgen','$evnvllnogen','$evndes','$tarx','$tary','OFC099','$datetime_now','OFC099','$datetime_now')";
		//echo "<br>".$sql;
		$rs2=mysql_query($sql);

		//insert srcdate and cause1
		$pol_recid=$row["pol_recid"];
		$pol_cause=$row["pol_cause"];
		if($pol_recid<>""){
			$sql="insert into evnsrc(evnid,srcid,evncasid,dtedtasrc) values('$evnid','P3','$pol_cause','$datetime_now')";
			//echo "<br>".$sql;
			$rs2=mysql_query($sql);
		}
		//insert srcdate and cause2
		$code_acc=$row["code_acc"];
		$mil_cause=$row["mil_cause"];
		if($code_acc<>""){
			$sql="insert into evnsrc(evnid,srcid,evncasid,dtedtasrc) values('$evnid','S3','$mil_cause','$datetime_now')";
			//echo "<br>".$sql;
			$rs2=mysql_query($sql);
		}
		//insert pol_typ
		$pol_typ=$row["pol_typ"];
		if($pol_typ<>""){
			$sql="insert into evnrelevntyp(evnid,evntypid) values('$evnid','$pol_typ')";
			//echo "<br>".$sql;
			$rs2=mysql_query($sql);
		}
		//insert typ1
		$type_acc1=$row["type_acc1"];
		$type_acc2=$row["type_acc2"];
		$type_acc3=$row["type_acc3"];
		if($type_acc1<>""){
			$sql="insert into evnrelevntyp(evnid,evntypid) values('$evnid','$type_acc1')";
			//echo "<br>".$sql;
			$rs2=mysql_query($sql);
		}
		if($type_acc2<>""){
			$sql="insert into evnrelevntyp(evnid,evntypid) values('$evnid','$type_acc2')";
			//echo "<br>".$sql;
			$rs2=mysql_query($sql);

		}
		if($type_acc3<>""){
			$sql="insert into evnrelevntyp(evnid,evntypid) values('$evnid','$type_acc3')";
			//echo "<br>".$sql;
			$rs2=mysql_query($sql);

		}
		//loop for insert victim
		$event_id=$row["event_id"];
		$sql_victim="select event_id,pid,prename,fname,sname,sts,left(residence,6) as rolplcidgen,right(residence,2) as rolvllnogen,sex,religion,occup from victim_daily where event_id='$event_id'";
		//echo "<br>วนลูป victim: ".$sql_victim;
		$rs_victim=mysql_query($sql_victim);
		while($row_victim = mysql_fetch_array($rs_victim)){//วนลูปเพิ่มผู้ได้รับผลกระทบ
			//insert person if not exist identify by pid
			$sql="select perid from person where persiden='".$row_victim["pid"]."'";
			//echo "<br>".$sql;
			$rs_person=mysql_query($sql);
			if($row_person=mysql_fetch_array($rs_person)){// หา perid
				//get perid
				$perid=$row_person["perid"];
			}else{ // หา perid และเพิ่ม person ใหม่
				//get new perid
				$sql = "select max(perid)+1 as new_perid from person";
				//echo "<br>".$sql;
				$rs_person=mysql_query($sql);
				$row_person = mysql_fetch_array($rs_person);
				$perid=$row_person["new_perid"];
				//insert person
				$persiden=$row_victim["pid"];
				$nme=$row_victim["fname"];
				$surnme=$row_victim["sname"];
				$rolplcidgen=$row_victim["rolplcidgen"];
				$rolvllnogen=$row_victim["rolvllnogen"];	
				$sex=$row_victim["sex"];
				$reg=$row_victim["religion"];
				$occid=$row_victim["occup"];		
				$sql="insert into person(perid,persiden,nme,surnme,rolplcidgen,rolvllnogen,sex,reg,occid,savofc,savdte,updofc,upddte) values($perid,'$persiden','$nme','$surnme','$rolplcidgen','$rolvllnogen','$sex','$reg','$occid','OFC099','$datetime_now','OFC099','$datetime_now')";
				//echo "<br>".$sql;
				$rs_person=mysql_query($sql);
				//inset person source //เ้ว้นไว้ก่อน//insert source of person use same event source
			}
			//insert losofper
			$losbody=$row_victim["sts"];
			$sql = "select max(losofperid)+1 as new_losofperid from losofper";
			//echo "<br>".$sql;
			$rs_person=mysql_query($sql);
			$row_person = mysql_fetch_array($rs_person);
			$new_losofperid=$row_person["new_losofperid"];
			$sql="insert into losofper(losofperid,evnid,perid,losbody) values($new_losofperid,$evnid,$perid,'$losbody')";
			//echo "<br>".$sql;
			$rs_person=mysql_query($sql);
		}//จบการวนลูปเพิ่มผู้ได้รับผลกระทบ
	}//จบการวนลูปเพิ่มเหตุการณ์
	//drop event_daily
	$sql = "drop table if exists event_daily;";
	$rs=mysql_query($sql);
	//drop victim_daily
	$sql = "drop table if exists victim_daily;";
	$rs=mysql_query($sql);
	mysql_close($c);
	echo "<br><br>complete Append To event,evnsrc,person,losofper From event_daily,victim_daily";
?>